package com.ar4j.bench.test.hibernate;

import javax.annotation.Resource;

import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.PlatformTransactionManager;

import com.ar4j.bench.common.domain.ICategory;
import com.ar4j.bench.common.domain.IItem;
import com.ar4j.bench.common.domain.IProduct;
import com.ar4j.bench.common.service.ICategoryService;
import com.ar4j.bench.common.service.IProductService;
import com.ar4j.bench.hibernate.domain.Category;
import com.ar4j.bench.hibernate.domain.Item;
import com.ar4j.bench.hibernate.domain.Product;
import com.ar4j.bench.hibernate.service.CategoryHibernateService;
import com.ar4j.bench.hibernate.service.ProductHibernateService;
import com.ar4j.bench.test.synthetic.AbstractSyntheticBenchmarkTest;

/**
 * A JUnit test for running the full benchmark suite for the Hibernate library.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/applicationContext-junit.xml" })
@TransactionConfiguration(transactionManager = "hibernateTransactionManager", defaultRollback = false)
public class HibernateSyntheticBenchmark extends AbstractSyntheticBenchmarkTest {
  
  @Autowired
  private CategoryHibernateService categoryService;

  @Autowired
  private ProductHibernateService productService;
  
  @Resource(name = "hibernateTransactionManager")
  private PlatformTransactionManager hibernateTxManager;
  
  @Override
  protected Class<? extends ICategory> getCategoryBaseClass() {
    return Category.class;
  }

  @Override
  protected ICategoryService getCategoryService() {
    return categoryService;
  }

  @Override
  protected Class<? extends IItem> getItemBaseClass() {
    return Item.class;
  }

  @Override
  protected String getName() {
    return "Hibernate";
  }

  @Override
  protected Class<? extends IProduct> getProductBaseClass() {
    return Product.class;
  }

  @Override
  protected IProductService getProductService() {
    return productService;
  }

  @Override
  protected PlatformTransactionManager getTxManager() {
    return hibernateTxManager;
  }

  @Override
  protected void prepareProduct(ICategory category, IProduct product) {
    Product theProduct = (Product) product;
    Category theCategory = (Category) category;
    
    theProduct.setCategory(theCategory);
  }
  
  @Override
  protected void prepareItem(IProduct product, IItem item) {
    Item theItem = (Item) item;
    Product theProduct = (Product) product;
    
    theItem.setProduct(theProduct);
  }

}
